#!/usr/bin/perl

# Global variables
$ttyport="/dev/tty.usbserial-A9007M6s";  # Port to listen on
$outfile1="/tmp/file1"; # File to write, player1
$outfile2="/tmp/file2"; # File to write, player2


# Open the tty
open DEVICE, "<$ttyport" or die "Unable to open $ttyport\n";
print "$ttyport opened.\n";

# Open the output files
open LOGFILE1, ">$outfile1" or die "Unable to open $outfile1 for writing!\n";
open LOGFILE2, ">$outfile2" or die "Unable to open $outfile2 for writing!\n";

# Loop and log input
while (<DEVICE>) {
   chomp;
   my $logline = $_;
   print "$logline\n";
   if ($logline =~ s/^P1://) { print LOGFILE1 "$logline\n"; } # Player 1
   if ($logline =~ s/^P2://) { print LOGFILE2 "$logline\n"; } # Player 2
} 

close(DEVICE);
close(LOGFILE);

